package com.hhf.rpc.core.proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Author hhf
 * @Date 2024/10/16
 * @Description
 */
@Slf4j
public class MockServiceProxy implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //1.获取方法的返回值
        Class<?> returnType = method.getReturnType();
        //2.日志记录
        log.info("mock service is running...");
        //3.返回指定的方法类型
        return getDefaultObject(returnType);
    }

    private Object getDefaultObject(Class<?> returnType) {

        if (returnType.isPrimitive()){
            if (returnType == Integer.class){
                return 0;
            } else if (returnType == Double.class) {
                return 0.0;
            } else if (returnType == Boolean.class){
                return false;
            } else if (returnType == Byte.class){
                return Byte.valueOf("mock");
            } else if (returnType == Short.class){
                return 0;
            } else if (returnType == Long.class){
                return 0L;
            } else if (returnType == Float.class){
                return 0.0;
            }
        }
        //对象类型
        return null;
    }
}
